package com.keyancompany.zhongbaopingtai.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 基于adapter的一个封装里边主要是增删改查的一些操作
 *
 * @param <T>
 * @author pengtao_H
 */
public abstract class ListAdapter<T> extends BaseAdapter {

    private List<T> items;
    Context ctx;
//	private LoadingHanlder handler;
//	private LoadingNoCancelHanlder noCancelHanlder;

    public ListAdapter(Context ctx) {
        super();
        this.ctx = ctx;
    }

    public ListAdapter(List<T> items, Context ctx) {
        super();
        this.items = items;
        this.ctx = ctx;
    }

    public Context getContext() {
        return ctx;
    }

//	public LoadingHanlder getLoading(int hint) {
//		if (handler == null)
//			handler = new LoadingHanlder(ctx);
//		if (hint != -1)
//			handler.setTitleId(hint);
//		return handler;
//	}
//
//	public LoadingNoCancelHanlder getLoadingNoCancel(int hint) {
//		if (noCancelHanlder == null) {
//			noCancelHanlder = new LoadingNoCancelHanlder(ctx);
//		}
//		// noCancelHanlder.setTargetLocation(view);
//		if (hint != -1)
//			noCancelHanlder.setTitleId(hint);
//		return noCancelHanlder;
//	}

    public void addItemsBefore(T item) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        if (!isSameItem(item)) {
            items.add(0, item);
        }
    }

    public void addItemsAfter(T item) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        if (!isSameItem(item))
            items.add(item);
        notifyDataSetChanged();
    }

    public void addItemTargetLocation(int position, T item) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        if (!isSameItem(item))
            items.add(position, item);
        notifyDataSetChanged();
    }

    public void addAllAfter(List<T> item) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        if (item != null) {
            items.addAll(item);
            notifyDataSetChanged();
        }
    }

    public void addAllBefore(List<T> item) {
        if (items == null) {
            items = new ArrayList<T>();
        }
        if (item != null) {
            items.addAll(0, item);
            notifyDataSetChanged();
        }
    }

    public void replace(int position, T item) {
        if (items == null)
            return;
        if (items.size() < position)
            return;
        items.set(position, item);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (items != null) {
            items.remove(position);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        if (items != null) {
            items.clear();
            notifyDataSetChanged();
        }
    }

    /**
     * 去重 这时候建议复写item的equls 方法
     *
     * @param item
     * @return
     */
    public boolean isSameItem(T item) {
        if (items != null) {
            return items.contains(item);
        }
        return false;
    }

    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public T getItem(int arg0) {
        return items == null ? null : items.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(int position, View contentView, ViewGroup parent) {
        HolderView view = null;
        if (contentView == null) {
            contentView = View.inflate(ctx, getContentViewId(), null);
            view = getHoldView(position, contentView, parent);
            contentView.setTag(view);
        } else
            view = (HolderView) contentView.getTag();
        setItemView(position, view, items == null ? null : items.get(position));
        return contentView;
    }

    /***
     * 设置 布局文件
     */
    public abstract int getContentViewId();

    /***
     * 初始化 布局 文件
     */
    public abstract HolderView getHoldView(int position, View contentView,
                                           ViewGroup parent);

    /***
     * 设置具体数值
     **/
    public abstract void setItemView(int position, HolderView contentView,
                                     T item);

    public abstract class HolderView {

    }

}
